﻿using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.SuJiaHaos.StockTakings;
using _2112A_2_MES.SuJiaHaos.ToolTypes;
using _2112A_2_MES.SuJiaHaos.WareHouse;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace _2112A_2_MES.SuJiaHao
{
    /// <summary>
    /// 工装夹具类型服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class ToolTypeService : ApplicationService, IToolTypeService
    {
        /// <summary>
        /// 销售退货仓储
        /// </summary>
        private readonly IRepository<ToolType, int> _toolTypeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toolTypeRepository"></param>
        public ToolTypeService(IRepository<ToolType, int> toolTypeRepository)
        {
            _toolTypeRepository = toolTypeRepository;
        }

        /// <summary>
        /// 获取工装夹具列表
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public async Task<ReturnSelect<object>> GetToolTypeList(int pageSize, int pageIndex)
        {
            //获取工装夹具列表
            var toolTypeList = await _toolTypeRepository.GetListAsync();

            //// 使用 WhereIf 进行条件筛选
            //var wareHouseLists = wareHouseList
            //    .WhereIf(!string.IsNullOrEmpty(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode))
            //    .WhereIf(!string.IsNullOrEmpty(warehouseName), x => x.WarehouseName.Contains(warehouseName))
            //    .ToList();

            // 计算总记录数和当前页记录
            var totalCount = toolTypeList.Count;
            var pageList = toolTypeList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<ToolType>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 库存盘点添加
        /// </summary>
        /// <param name="input">库存盘点实体</param>
        /// <returns></returns>
        public async Task<int> GetCreateToolType(CreateToolTypeDTO input)
        {
            var toolTypes = ObjectMapper.Map<CreateToolTypeDTO, ToolType>(input);
            toolTypes.CreateBy = "崔二强";
            toolTypes.CreateTime = DateTime.Now;
            await _toolTypeRepository.InsertAsync(toolTypes);
            return toolTypes != null ? 1 : 0;
        }

        /// <summary>
        /// 销售退货删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteToolType(int id)
        {
            var toolTypeDel = await _toolTypeRepository.GetAsync(id);
            await _toolTypeRepository.DeleteAsync(toolTypeDel);
            return toolTypeDel != null ? 1 : 0;
        }

        /// <summary>
        /// 工装夹具反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回客户信息实体</returns>
        public async Task<ToolType> GetToolType(int id)
        {
            var toolTypes = await _toolTypeRepository.FirstOrDefaultAsync(x => x.Id == id);
            return toolTypes;
        }

        /// <summary>
        /// 工装夹具修改
        /// </summary>
        /// <param name="input">工装夹具实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateToolType(CreateToolTypeDTO input)
        {
            var toolTypes = ObjectMapper.Map<CreateToolTypeDTO, ToolType>(input);
            toolTypes.UpdateBy = "崔大强";
            toolTypes.UpdateTime = DateTime.Now;
            await _toolTypeRepository.UpdateAsync(toolTypes);
            return toolTypes != null ? 1 : 0;
        }
    }
}
